package com.wjp.villagemp.controller.manage;

import com.wjp.villagemp.model.manage.ClassEntity;
import com.wjp.villagemp.model.sys.PageResult;
import com.wjp.villagemp.service.manage.ClassService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班级管理
 * @author lixr
 */
@RestController
@RequestMapping("/class")
public class ClassController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ClassService classService;

    /**
     * 分页查询所有班级信息
     * @param pageSize
     * @param page
     * @param classNum
     * @return
     */
    @GetMapping
    public PageResult classList(int pageSize, int page, @RequestParam(required = false) int classNum) {
        PageResult pageResult = new PageResult();
        try {
            List<ClassEntity> listClass = classService.listClass(pageSize, page * pageSize, classNum);
            pageResult.setData(listClass);
            Integer classCount = classService.countClass(classNum);
            pageResult.setTotalCount(classCount);
        } catch (Exception e) {
            log.error("分页查询所有班级信息异常：" + e.getMessage());
        }
        return pageResult;
    }


    /**
     * 新增班级信息
     * @param classEntity
     * @return
     */
    @PostMapping
    public Map<String, Object> insertClass(@RequestBody ClassEntity classEntity) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (classEntity != null) {
                Integer key = classService.insertClass(classEntity);
                if (key > 0) {
                    resMap.put("msg", "success");
                } else {
                    resMap.put("msg", "fail");
                }
            }
        } catch (Exception e) {
            log.error("新增班级信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 更新班级信息
     * @param classEntity
     * @return
     */
    @PutMapping
    public Map<String, Object> updateClass(@RequestBody ClassEntity classEntity) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (classEntity != null) {
                Integer key = classService.updateClass(classEntity);
                if (key > 0) {
                    resMap.put("msg", "success");
                } else {
                    resMap.put("msg", "fail");
                }
            }
        } catch (Exception e) {
            log.error("更新班级信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 批量删除班级信息
     * @param groupId
     * @return
     */
    @DeleteMapping
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> removeClass(@RequestBody List<Integer> groupId) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (groupId.size() >= 1) {
                boolean flag = false;
                for (Integer classNum : groupId) {
                    Integer key = classService.removeClass(classNum);
                    if (key > 0) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                }
                if (flag) {
                    resMap.put("msg", "success");
                }
            }
        } catch (Exception e) {
            log.error("批量删除班级信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 查询全部班级信息
     * @return
     */
    @GetMapping("/getAllClasses")
    public List<ClassEntity> getAllClasses() {
        List<ClassEntity> classList = null;
        try {
            classList  = classService.listAllClass();
        } catch (Exception e) {
            log.error("查询全部班级信息异常：" + e.getMessage());
        }
        return classList;
    }
}
